/*
 * =====================================================================================
 *
 *       Filename:  assign.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  05/26/2011 06:14:57 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */

#include <iostream>
#include <string>
using namespace std;
#include "gtest/gtest.h"
#include "List02.hpp"

TEST(assignTest, inputIterator)
{
	List<int> int_list;
	int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	const size_t arr_size = sizeof(arr) / sizeof(int);

	int_list.assign(arr, arr + arr_size);
	EXPECT_EQ(int_list.size(), arr_size);
	int_list.assign(arr, arr);
	EXPECT_EQ(int_list.size(), 0u);
	int_list.assign(arr, arr + arr_size);
	EXPECT_EQ(int_list.size(), arr_size);

	int cnt = 0;
	for (List<int>::iterator it = int_list.begin();
			it != int_list.end(); ++it)
		EXPECT_EQ(*it, cnt++);
	int_list.assign(int_list.begin(), int_list.end());
	EXPECT_EQ(int_list.size(), arr_size);

	List<int>::iterator it = int_list.begin();
	++it;
	++it;
	int_list.assign(int_list.begin(), it);
	EXPECT_EQ(int_list.size(), 2u);
	cnt = 0;
	for (List<int>::iterator it = int_list.begin();
			it != int_list.end(); ++it)
		EXPECT_EQ(*it, cnt++);

	// string
	const char * str[] = {
		"hello, ",
		"the ",
		"world"
	};
	const size_t str_len = sizeof(str) / sizeof(const char *);
	List<string> str_list(str, str + str_len);
	EXPECT_EQ(str_len, str_list.size());
	string hello;
	for (List<string>::iterator it = str_list.begin();
			it != str_list.end(); ++it)
		hello += *it;
	EXPECT_STREQ("hello, the world", hello.c_str());

	str_list.assign(str, str + str_len);
	EXPECT_EQ(str_len, str_list.size());
	hello = "";
	for (List<string>::iterator it = str_list.begin();
			it != str_list.end(); ++it)
		hello += *it;
	EXPECT_STREQ("hello, the world", hello.c_str());
}


TEST(assignTest, repetitiveSequence)
{
	List<int> int_list;

	int_list.assign(10u, 10);
	EXPECT_EQ(10u, int_list.size());
	for (List<int>::iterator it = int_list.begin();
			it != int_list.end(); ++it)
		EXPECT_EQ(*it, 10);
	int_list.assign(0u, 10);
	EXPECT_EQ(0u, int_list.size());
	int_list.assign(10u, 10);
	EXPECT_EQ(10u, int_list.size());
	for (List<int>::iterator it = int_list.begin();
			it != int_list.end(); ++it)
		EXPECT_EQ(*it, 10);

	// string
	List<string> str_list(3u, "hh ");
	string hh = "hh ";
	for (List<string>::iterator it = str_list.begin();
			it != str_list.end(); ++it)
		EXPECT_STREQ(hh.c_str(), it->c_str());
	EXPECT_EQ(3u, str_list.size());

	
}

TEST(assignTest, example)
{
	List<int> first;
	List<int> second;

	first.assign (7u,100);                      // 7 ints with value 100

	second.assign (first.begin(),first.end()); // a copy of first

	int myints[]={1776,7,4};
	first.assign (myints,myints+3);            // assigning from array
	
	EXPECT_EQ(7u, second.size());
	EXPECT_EQ(3u, first.size());

}
